﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MaLu.Data;
using System.Data;
using MaLu.Common;
using System.Xml;
using MaLu.BusiLogic;
using MaLu.Framework.Common;

namespace MaluJg.web.Visite
{
    public partial class item3 : VisitePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                if (VisiteID != Guid.Empty)
                {

                    DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from Visite where id ='" + VisiteID.ToString() + "'");
                    if (dt.Rows.Count > 0)
                    {
                        //lbLeader.Text = dt.Rows[0]["Leader"].ToString();
                        lbDept2.Text = dt.Rows[0]["Interviewee"].ToString();
                        lbYear.Text = dt.Rows[0]["Year"].ToString();
                        lbMonth.Text = dt.Rows[0]["Month"].ToString();
                        tbMonth1.Text = lbMonth.Text;
                    }
                }
                BindData();
            }
        }
        private void BindData()
        {
            string projectname = string.Empty;
            string content = BLVisite.GetVisiteItem(Provider, VisiteID, "item3.aspx", ref projectname);
            if (content != string.Empty)
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(content);
                XmlElement xe = document.SelectSingleNode("//记录人") as XmlElement;
                tbRecord.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//单位") as XmlElement;
                tbDept1.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//月1") as XmlElement;
                tbMonth1.Text = Util.ReturnXml(xe.InnerText);

                xe = document.SelectSingleNode("//年2") as XmlElement;
                tbYear2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//月2") as XmlElement;
                tbMonth2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//日2") as XmlElement;
                tbDay2.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//问题") as XmlElement;

                XmlNodeList xnl = xe.ChildNodes;
                if (xnl.Count >= 1)
                {
                    tbP00.Text = xnl[0].Attributes["n"].Value;
                    tbP01.Text = xnl[0].Attributes["d"].Value;
                }

                if (xnl.Count >= 2)
                {
                    tbP10.Text = xnl[1].Attributes["n"].Value;
                    tbP11.Text = xnl[1].Attributes["d"].Value;
                }

                if (xnl.Count >= 3)
                {
                    tbP20.Text = xnl[2].Attributes["n"].Value;
                    tbP21.Text = xnl[2].Attributes["d"].Value;
                }

                if (xnl.Count >= 4)
                {
                    tbP30.Text = xnl[3].Attributes["n"].Value;
                    tbP31.Text = xnl[3].Attributes["d"].Value;
                }

                if (xnl.Count >= 5)
                {
                    tbP40.Text = xnl[4].Attributes["n"].Value;
                    tbP41.Text = xnl[4].Attributes["d"].Value;
                }

                if (xnl.Count >= 6)
                {
                    tbP50.Text = xnl[5].Attributes["n"].Value;
                    tbP51.Text = xnl[5].Attributes["d"].Value;
                }

                if (xnl.Count >= 7)
                {
                    tbP60.Text = xnl[6].Attributes["n"].Value;
                    tbP61.Text = xnl[6].Attributes["d"].Value;
                }

                if (xnl.Count >= 8)
                {
                    tbP70.Text = xnl[7].Attributes["n"].Value;
                    tbP71.Text = xnl[7].Attributes["d"].Value;
                }

            }
        
        }
        private string FillContent()
        {
            string template = BLVisite.GetTemplate(Provider, "item3.aspx");
            if (template != string.Empty)
            {

                XmlDocument document = new XmlDocument();
                document.LoadXml(template);
                XmlElement xe = document.SelectSingleNode("//记录人") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbRecord.Text.Trim());
                xe = document.SelectSingleNode("//单位") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDept1.Text.Trim());
                xe = document.SelectSingleNode("//月1") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth1.Text.Trim());
                xe = document.SelectSingleNode("//年2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbYear2.Text.Trim());
                xe = document.SelectSingleNode("//月2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbMonth2.Text.Trim());
                xe = document.SelectSingleNode("//日2") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbDay2.Text.Trim());
                xe = document.SelectSingleNode("//问题") as XmlElement;
                XmlNode xn = document.CreateElement("p");
                XmlAttribute xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP00.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP01.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                 xn = document.CreateElement("p");
                 xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP10.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP11.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP20.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP21.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP30.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP31.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP40.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP41.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP50.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP51.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP60.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP61.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                xn = document.CreateElement("p");
                xattr = document.CreateAttribute("n");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP70.Text));
                xn.Attributes.Append(xattr);
                xattr = document.CreateAttribute("d");
                xattr.Value = string.Format("{0}", Util.ReturnXml(tbP71.Text));
                xn.Attributes.Append(xattr);
                xe.AppendChild(xn);

                
                return document.OuterXml;
            }
            else
                return string.Empty;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLVisite.SaveVisiteItem(Provider, VisiteID, "item3.aspx", FillContent());


            string msg = "已保存!";

            string script = "<script>alert('" + msg + "');</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "Close", script);


        }
    }
}
